package com.avocado.auth.server.service;


import com.avocado.base.entity.Resources;
import com.avocado.base.entity.Role;
import com.avocado.base.entity.User;
import com.avocado.base.entity.UserRole;
import com.avocado.base.repository.ResourcesRepository;
import com.avocado.base.repository.UserRepository;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;



import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 
 * @author xuwenfeng
 *
 */
@Service
public class CustomUserDetailServiceImpl implements UserDetailsService {


    private final UserRepository userRepository;
    

    private final ResourcesRepository resourcesRepository;

    public CustomUserDetailServiceImpl(UserRepository userRepository, ResourcesRepository resourcesRepository) {
        this.userRepository = userRepository;
        this.resourcesRepository=resourcesRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       
        
        List<User> userList=userRepository.findAllByName(username);
        
        if (userList.isEmpty()) {
            throw new UsernameNotFoundException(username);
        }
        Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
        // 可用性 :true:可用 false:不可用
        boolean enabled = true;
        // 过期性 :true:没过期 false:过期
        boolean accountNonExpired = true;
        // 有效性 :true:凭证有效 false:凭证无效
        boolean credentialsNonExpired = true;
        // 锁定性 :true:未锁定 false:已锁定
        boolean accountNonLocked = true;
        
        User user=userList.get(0);
        
        Set<UserRole> userRolesSet=user.getUserRoles();
        
        List<Resources> resourcesList=resourcesRepository.findAll(user.getName());
        
        for (UserRole userRole : userRolesSet) {
        	
        	Role role=userRole.getRole();
        	
            //角色必须是ROLE_开头，可以在数据库中设置
            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role.getCode());

            grantedAuthorities.add(grantedAuthority);
            //获取权限
            for (Resources permission :resourcesList) {

            	if(!StringUtils.isEmpty(permission.getCode())) {

            		 GrantedAuthority authority = new SimpleGrantedAuthority(permission.getCode());

                     grantedAuthorities.add(authority);
            	}
               
            }
        }

        return  new org.springframework.security.core.userdetails.User(user.getName(),user.getPassword(),
        
        		enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuthorities);

    }

	

}

